<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/Blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/Blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/Blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/Blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/Blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/Blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/Blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="JavaScript," />










<meta name="description" content="js 计算失误情况​ 在用 js 进行数值计算的时候，比如0.1+0.2时，会产生精度问题。另一方面，number 的计算范围有限，当 number 用来计算整数时，其范围为-2^53 ~ 2^53,为 16 位数。当数字超过 16 位数时，js 既不会报错，会造成的影响只是计算结果不准确；另外，当数字是小于 1 且小数点后面带有 6 个 0 以上的浮点数值或者整数位数字多于 21 位，js 会自">
<meta property="og:type" content="article">
<meta property="og:title" content="金融大数高精度计算解决方案">
<meta property="og:url" content="https://jingping-ye.github.io/Blog/2019/09/28/%E9%87%91%E8%9E%8D%E5%A4%A7%E6%95%B0%E9%AB%98%E7%B2%BE%E5%BA%A6%E8%AE%A1%E7%AE%97%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/index.html">
<meta property="og:site_name" content="jingping.ye">
<meta property="og:description" content="js 计算失误情况​ 在用 js 进行数值计算的时候，比如0.1+0.2时，会产生精度问题。另一方面，number 的计算范围有限，当 number 用来计算整数时，其范围为-2^53 ~ 2^53,为 16 位数。当数字超过 16 位数时，js 既不会报错，会造成的影响只是计算结果不准确；另外，当数字是小于 1 且小数点后面带有 6 个 0 以上的浮点数值或者整数位数字多于 21 位，js 会自">
<meta property="article:published_time" content="2019-09-28T02:25:00.000Z">
<meta property="article:modified_time" content="2020-05-14T05:58:32.962Z">
<meta property="article:author" content="jingping.ye">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/Blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://jingping-ye.github.io/Blog/2019/09/28/金融大数高精度计算解决方案/"/>





  <title>金融大数高精度计算解决方案 | jingping.ye</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/Blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">jingping.ye</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">jingping.ye的Blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/Blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/Blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/Blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/Blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://jingping-ye.github.io/Blog/Blog/2019/09/28/%E9%87%91%E8%9E%8D%E5%A4%A7%E6%95%B0%E9%AB%98%E7%B2%BE%E5%BA%A6%E8%AE%A1%E7%AE%97%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="jingping.ye">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/Blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="jingping.ye">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">金融大数高精度计算解决方案</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-28T10:25:00+08:00">
                2019-09-28
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2020-05-14T13:58:32+08:00">
                2020-05-14
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/Blog/categories/%E6%8A%80%E6%9C%AF/" itemprop="url" rel="index">
                    <span itemprop="name">技术</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/Blog/categories/%E6%8A%80%E6%9C%AF/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index">
                    <span itemprop="name">前端</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="js-计算失误情况"><a href="#js-计算失误情况" class="headerlink" title="js 计算失误情况"></a>js 计算失误情况</h2><p>​ 在用 js 进行数值计算的时候，比如<code>0.1+0.2</code>时，会产生精度问题。另一方面，number 的计算范围有限，当 number 用来计算整数时，其范围为<code>-2^53 ~ 2^53</code>,为 16 位数。当数字超过 16 位数时，js 既不会报错，会造成的影响只是计算结果不准确；另外，当数字是小于 1 且小数点后面带有 6 个 0 以上的浮点数值或者整数位数字多于 21 位，js 会自动将数字转为科学计数法。</p>
<p>​ 为了解决这种情况，我们可以使用以下方法：</p>
<p>​ 1. 输入数字的时候尽量限制范围，防止用户输入过大不合理的数字</p>
<ol start="2">
<li><p>如果有产生由用户输出数字计算出来的数字，要校验该数字是否超过数据库允许的范围。比如进行乘操作的时候。</p>
</li>
<li><p>数据库中，以分为单位进行存储，避免存储小数。</p>
</li>
<li><p>计算数据时，不要使用<code>parseFloat</code>和<code>toFixed</code>相关 Number 对象方法进行计算，而是应该使用专门的库进行计算，比如此处说的 decimal.js 库</p>
</li>
</ol>
<h2 id="decimal-js"><a href="#decimal-js" class="headerlink" title="decimal.js"></a>decimal.js</h2><blockquote>
<p>github: <a href="https://github.com/MikeMcl/decimal.js" target="_blank" rel="noopener">https://github.com/MikeMcl/decimal.js</a></p>
</blockquote>
<p>decimal.js 是使用的二进制来计算的数字处理库，以下是基本运算:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 加法</span></span><br><span class="line"><span class="keyword">const</span> c = <span class="keyword">new</span> Decimal(a).plus(<span class="keyword">new</span> Decimal(b));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 减法</span></span><br><span class="line"><span class="keyword">const</span> d = <span class="keyword">new</span> Decimal(a).sub(<span class="keyword">new</span> Decimal(b));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 乘法</span></span><br><span class="line"><span class="keyword">const</span> e = <span class="keyword">new</span> Decimal(a).mul(<span class="keyword">new</span> Decimal(b));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 除法</span></span><br><span class="line"><span class="keyword">const</span> f = <span class="keyword">new</span> Decimal(a).div(<span class="keyword">new</span> Decimal(b));</span><br></pre></td></tr></table></figure>

<p>另外，Decimal 有自己专门的<code>toString</code>和<code>toFixed</code>方法。当然，decimal.js 还有很多其他的十分强大的功能。</p>
<h2 id="DemCal"><a href="#DemCal" class="headerlink" title="DemCal"></a>DemCal</h2><p>以下是根据 decimal.js 封装的一些进行计算时常用到的函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Decimal = <span class="built_in">require</span>(<span class="string">"./decimal.min.js"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 转换为Decimal类型</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>_num 传入的数字，如果在js的有效范围外，那么传入必须是整数，否则会发生数据小数位丢失问题</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>_fixedDecimal 需要保留的小数位，默认为0，即整数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>_isToStr 是否需要转换为string返回，默认返回Decimal类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> toDecimal = <span class="function">(<span class="params">num, fixedDecimal = <span class="number">0</span>, isToStr = <span class="literal">false</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 为什么不适用toString,或者String =&gt; 对于过大的数字,toString或者String会将其转换成科学计数法,比如1111111111111111111111111111111111111111会转变为"1.1111111111111112e+39"</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">let</span> _num = <span class="string">""</span> + num;</span><br><span class="line">  <span class="keyword">let</span> _fixedDecimal = fixedDecimal;</span><br><span class="line">  _num = <span class="keyword">new</span> Decimal(_num).toFixed(_fixedDecimal);</span><br><span class="line">  <span class="keyword">return</span> isToStr ? _num.toString() : <span class="keyword">new</span> Decimal(_num);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 两数相加</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>num1 传入的数字1</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>num2 传入的数字2</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>fixedDecimal 需要保留的小数位，默认为0, 此处四舍五入</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>isToStr 是否需要转换为string返回，默认返回Decimal类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> plus = <span class="function">(<span class="params">num1, num2, fixedDecimal = <span class="number">0</span>, isToStr = <span class="literal">false</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> _num1 = toDecimal(num1);</span><br><span class="line">  <span class="keyword">const</span> _num2 = toDecimal(num2);</span><br><span class="line">  <span class="keyword">const</span> res = _num1.add(_num2).toFixed(fixedDecimal);</span><br><span class="line">  <span class="keyword">return</span> isToStr ? res.toString() : <span class="keyword">new</span> Decimal(res);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 两数相减</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>num1 传入的数字1</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>num2 传入的数字2</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>fixedDecimal 需要保留的小数位，此处四舍五入</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>isToStr 是否需要转换为string返回，默认返回Decimal类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> sub = <span class="function">(<span class="params">num1, num2, fixedDecimal = <span class="number">0</span>, isToStr = <span class="literal">false</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> _num1 = toDecimal(num1);</span><br><span class="line">  <span class="keyword">const</span> _num2 = toDecimal(num2);</span><br><span class="line">  <span class="keyword">const</span> res = _num1.sub(_num2).toFixed(fixedDecimal);</span><br><span class="line">  <span class="keyword">return</span> isToStr ? res.toString() : <span class="keyword">new</span> Decimal(res);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 两数相除</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>_num1 传入的数字1</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>_num2 传入的数字2</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>_fixedDecimal 需要保留的小数位，此处四舍五入</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>isToStr 是否需要转换为string返回，默认返回Decimal类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> div = <span class="function">(<span class="params">num1, num2, fixedDecimal = <span class="number">0</span>, isToStr = <span class="literal">false</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> _num1 = toDecimal(num1);</span><br><span class="line">  <span class="keyword">const</span> _num2 = toDecimal(num2);</span><br><span class="line">  <span class="keyword">const</span> res = _num1.div(_num2).toFixed(fixedDecimal);</span><br><span class="line">  <span class="keyword">return</span> isToStr ? res.toString() : <span class="keyword">new</span> Decimal(res);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 两数相乘</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>_num1 传入的数字1</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>_num2 传入的数字2</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>_fixedDecimal 需要保留的小数位，此处四舍五入</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>isToStr 是否需要转换为string返回，默认返回Decimal类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> mul = <span class="function">(<span class="params">num1, num2, fixedDecimal = <span class="number">0</span>, isToStr = <span class="literal">false</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> _num1 = toDecimal(num1);</span><br><span class="line">  <span class="keyword">const</span> _num2 = toDecimal(num2);</span><br><span class="line">  <span class="keyword">const</span> res = _num1.mul(_num2).toFixed(fixedDecimal);</span><br><span class="line">  <span class="keyword">return</span> isToStr ? res.toString() : <span class="keyword">new</span> Decimal(res);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 累加</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;array&#125;</span> </span>_numList 数字列表</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>_fixedDecimal 需要保留的小数位，此处四舍五入</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>isToStr 是否需要转换为string返回，默认返回Decimal类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> sum = <span class="function">(<span class="params">numList, fixedDecimal = <span class="number">0</span>, isToStr = <span class="literal">false</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> res = numList.reduce(<span class="function">(<span class="params">prev, next</span>) =&gt;</span> plus(prev, next));</span><br><span class="line">  <span class="keyword">return</span> isToStr ? toDecimal(res, fixedDecimal, <span class="literal">true</span>) : toDecimal(res, fixedDecimal);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 用千分符分隔数字</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;string/number&#125;</span> </span>num 数字</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> </span>fixedDecimal 需要保留的小数位，此处四舍五入</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;string&#125;</span> </span>返回带千分符分隔的字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">const</span> numByThousandDelimiter = <span class="function">(<span class="params">num, fixedDecimal = <span class="number">0</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> _num = toDecimal(num, fixedDecimal, <span class="literal">true</span>);</span><br><span class="line">  <span class="keyword">const</span> decimalReg = <span class="regexp">/(\d)(?=(\d&#123;3&#125;)+\.)/g</span>;</span><br><span class="line">  <span class="keyword">const</span> intReg = <span class="regexp">/(?=(?!\b)(\d&#123;3&#125;)+$)/g</span>;</span><br><span class="line">  <span class="keyword">return</span> _num.indexOf(<span class="string">"."</span>) !== <span class="number">-1</span> ? _num.replace(decimalReg, <span class="string">"$1,"</span>) : _num.replace(intReg, <span class="string">","</span>);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> financeTool = &#123;</span><br><span class="line">  toDecimal: toDecimal,</span><br><span class="line">  plus: plus,</span><br><span class="line">  sub: sub,</span><br><span class="line">  div: div,</span><br><span class="line">  mul: mul,</span><br><span class="line">  sum: sum,</span><br><span class="line">  numByThousandDelimiter: numByThousandDelimiter,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = financeTool;</span><br></pre></td></tr></table></figure>

<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> financeTool = <span class="built_in">require</span>(<span class="string">"./financeTool"</span>);</span><br><span class="line"><span class="built_in">console</span>.log(financeTool.add(<span class="number">1.1</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="literal">true</span>));</span><br></pre></td></tr></table></figure>

<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p><a href="https://book.douban.com/subject/1231579/" target="_blank" rel="noopener">javascript 权威指南</a></p>
<p><a href="http://mikemcl.github.io/decimal.js/" target="_blank" rel="noopener">decimal.js API</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/Blog/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/Blog/2019/09/14/%E4%BD%BF%E7%94%A8Fiddler%E6%8A%93%E5%8F%96%E5%BE%AE%E4%BF%A1%E9%A5%BF%E4%BA%86%E4%B9%88%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%95%B0%E6%8D%AE/" rel="next" title="使用Fiddler抓取微信饿了么小程序数据">
                <i class="fa fa-chevron-left"></i> 使用Fiddler抓取微信饿了么小程序数据
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/Blog/2019/10/10/%E8%A1%A8%E5%8D%95%E9%AA%8C%E8%AF%81%E5%B7%A5%E5%85%B7/" rel="prev" title="表单验证工具">
                表单验证工具 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">jingping.ye</p>
              <p class="site-description motion-element" itemprop="description">jingping.ye的Blog|文章</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/Blog/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/Blog/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/Blog/tags/index.html">
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yourname" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:yourname@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/yourname" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yourname" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.facebook.com/yourname" target="_blank" title="FB Page">
                      
                        <i class="fa fa-fw fa-facebook"></i>FB Page</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://vk.com/yourname" target="_blank" title="VK Group">
                      
                        <i class="fa fa-fw fa-vk"></i>VK Group</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://stackoverflow.com/yourname" target="_blank" title="StackOverflow">
                      
                        <i class="fa fa-fw fa-stack-overflow"></i>StackOverflow</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://youtube.com/yourname" target="_blank" title="YouTube">
                      
                        <i class="fa fa-fw fa-youtube"></i>YouTube</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://instagram.com/yourname" target="_blank" title="Instagram">
                      
                        <i class="fa fa-fw fa-instagram"></i>Instagram</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="skype:yourname?call|chat" target="_blank" title="Skype">
                      
                        <i class="fa fa-fw fa-skype"></i>Skype</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#js-计算失误情况"><span class="nav-number">1.</span> <span class="nav-text">js 计算失误情况</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#decimal-js"><span class="nav-number">2.</span> <span class="nav-text">decimal.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DemCal"><span class="nav-number">3.</span> <span class="nav-text">DemCal</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用"><span class="nav-number">3.1.</span> <span class="nav-text">使用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考"><span class="nav-number">4.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">jingping.ye</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">15k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/Blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/Blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/Blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/Blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/Blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/Blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/Blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
